<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 标
 * @author chenhuan
 *
 */
class Borrow extends MY_Controller {
	
	function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 标列表
	 * @param $qtype 查询类型
	 */
	function zhs_blist(){
		//搜索参数
		/* $per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$keywords 			= $this->input->get('keywords');
		$time_limit 		= $this->input->get('time_limit');
		$ftype 				= $this->input->get('ftype'); */
		
		$postArr = array(
			'sftype' => intval($this->input->get('sftype')),//标的类型
			'saccount' => intval($this->input->get('saccount')),//金额
			'sapr' => intval($this->input->get('sapr')),//年化利率
			'slimit' => intval($this->input->get('slimit')),//期限
			'style' => intval($this->input->get('style')),//还款方式
			'srate' => intval($this->input->get('srate')),//还款方式
			'sorder' => $this->input->get('sorder'),//
		);
		
		$postArr['sorder'] = $postArr['sorder']?$postArr['sorder']:'all';
		
		$vdata['postArr'] = $postArr;
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		$this->load->model('Credit_model');	
		
		$data['where']['sftype'] 		= $postArr['sftype'];
		$data['where']['saccount'] 		= $postArr['saccount'];
		$data['where']['sapr'] 			= $postArr['sapr'];
		$data['where']['slimit'] 		= $postArr['slimit'];
		$data['where']['style'] 		= $postArr['style'];
		$data['where']['srate'] 		= $postArr['srate'];
		$data['where']['sorder'] 		= $postArr['sorder'];
		$per_page = $this->input->get('per_page')?intval($this->input->get('per_page')):2;
		$data['page_start'] = $per_page;//intval($page);
		$data['page_size'] = 10;
		$data['list'] = 0;
		
		$retData = $this->Borrow_model->get_borrow_blist($data);
		$vdata['borrow_list'] = $retData['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/borrow/zhs_blist.html?sftype='.$postArr['sftype'].'&saccount='.$postArr['saccount'].'&sapr='.$postArr['sapr'].'&slimit='.$postArr['slimit'].'&style='.$postArr['style'].'&srate='.$postArr['srate'];
		$config['total_rows'] = $retData['borrow_count'];
		$config['per_page'] = $data['page_size']; 
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		
		//获取用户资金信息
		if($this->session->userdata('rsh_user') != null){
			$user = $this->session->userdata('rsh_user');
			$this->load->model('Account_model');
			$vdata['user_account'] 	= $this->Account_model->get_account($user['user_id']);
			$tuser_account 			= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}else{
				$vdata['user_paypassword'] = $tuser_account['paypassword'];
			}
		}
		$borrow_list = array();
		foreach ($vdata['borrow_list'] as $borrow){
			if ($borrow['ftype']!=6){
				$user_id = $borrow['user_id'];
				$user_obj = $this->User_model->get_by_uid($user_id);
				$borrow['username'] = $user_obj['username'];
				$borrow['province'] = $user_obj['province'];
				$borrow['city'] 	= $user_obj['city'];
				$borrow['area'] 	= $user_obj['area'];
				$credit_obj = $this->Credit_model->get_row($user_id);
				$credit_obj['credit_value'] = isset($credit_obj) ? $credit_obj['credit_value'] : 0;
				$borrow['user_credit'] 		= $credit_obj['credit_value'];
				$borrow['user_credit_img'] 	= $this->Credit_model->get_pic($credit_obj['credit_value']);
				$borrow_list[] 		= $borrow;
			}
		}
		
		//获取地区信息
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$vdata['borrow_list']				= $borrow_list;
		
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_account'] 			= $this->Borrow_model->arrays('borrow_account');// 理财金额
		$vdata['borrow_status'] 			= $this->Borrow_model->arrays('borrow_status');//状态
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
		$vdata['borrow_time_limit_query'] 		= $this->Borrow_model->arrays('borrow_time_limit_query');//理财期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		/*
			$vdata['time_limit'] 	= $time_limit;
			$vdata['ftype'] 		= $ftype;
			$vdata['qtype'] 		= $qtype;
			$vdata['keywords'] 		= $keywords;
		*/
		
		$vdata['title'] 		= "理财产品".' - '.CON_KEYWORDS;
		$vdata['tab'] 		= "tender";
		
		$vdata['all_order'] = 'all';
		$vdata['account_order'] = $postArr['sorder'] && $postArr['sorder']=="account_down"? 'account_up':'account_down';
		$vdata['apr_order'] = $postArr['sorder'] && $postArr['sorder']=="apr_down"? 'apr_up':'apr_down';
		$vdata['timelimit_order'] = $postArr['sorder'] && $postArr['sorder']=="timelimit_down"? 'timelimit_up':'timelimit_down';
		
		// 级联菜单
		$vdata['sub_1'] = "理财产品列表";
		$vdata['sub_1_href'] = "/borrow/zhs_blist.html";

		$this->load->view('borrow/zhs_borrow_list', $vdata);
	}
	
	
	/**
	 * 标列表
	 * @param $qtype 查询类型
	 */
	function blate($qtype = 'late'){
		//搜索参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$keywords 			= $this->input->get('keywords');
		$time_limit 		= $this->input->get('time_limit');
		$ftype 				= $this->input->get('ftype');
		
		$data['page_size']  			= BORROW_LIST_PAGE_SIZE;
		$data['page_start'] 			= (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_late_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/borrow/blate/index.html';
		$config['total_rows'] = $vdata['late_count'];
		$config['per_page'] = BORROW_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		
		//获取地区信息
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//理财期限
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//理财期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_valid_time'] 		= $this->Borrow_model->arrays('borrow_valid_time');//有效时间
		$vdata['qtype'] 		= "late";
		$vdata['time_limit'] 	= $time_limit;
		$vdata['ftype'] 		= $ftype;
		$vdata['keywords'] 		= $keywords;
		$vdata['title'] 		= "我要理财".' - '.CON_KEYWORDS;
		$this->load->view('borrow/borrow_list', $vdata);
	}
	

	//20141129 by liub 查看标的详情*********************
	//****** 同步更新  zhs_obj 方法
	function obj($id){
		self::zhs_obj($id);
	}
	
	

	//20141129 by liub 查看标的详情
	function zhs_obj($id){
		
		$this->load->model('User_model');
		$this->load->model('User_info_model');
		$this->load->model('Area_model', 'area');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		$this->load->model('Credit_model');
		$this->load->model('User_amount_model');
		$this->load->model('Attestation_model');
		
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_work_time'] 			= $this->Borrow_model->arrays('borrow_work_time');//计息方式
		$vdata['borrow_fuse'] 				= $this->Borrow_model->arrays('borrow_fuse');//资金用途
		$vdata['borrow_good'] 				= $this->Borrow_model->arrays('borrow_good');
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		if(!$borrow_obj){
			$this->js->alert('理财标不存在');
			$this->js->go('/');
			return;
		}
		if($borrow_obj['fstatus'] !=1 && $borrow_obj['fstatus'] !=3){
			$this->js->alert("您没有权限访问该理财标");
			$this->js->go("/borrow/zhs_blist/index.html");
			return;
		}
		$vdata['borrow_style'] 		= $this->Borrow_model->arrays('borrow_style');//还款方式
		$user = $this->session->userdata('rsh_user');

		//修改标的点击次数
		//$this->Borrow_model->update_hits($id);
	
		$user_account = array();
		$tuser_account = array();
		$vdata['key_red']  = 0;//可激活代金卷
		if($user == null){
			$user_account['total'] 		= 0;
			$user_account['use_money'] 	= 0;
			$tuser_account['paypassword'] = "";
		}else{
			$user_account 					= $this->Account_model->get_account($user['user_id']);
			if($user_account == false){
				$user_account['total'] 		= 0;
				$user_account['use_money'] 	= 0;
			}
			$tuser_account 					= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*100;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$vdata['key_red'] = floor($key_money/200);
			if(!isset($user_account['red_money'])) $user_account['red_money'] =0;
			/*end */
		}
		
			
		$vdata['user_account']			= $user_account;
		
		$vdata['tuser_account']			= $tuser_account;
		//TODO 20150414 疑问
		//$vdata['user_name_list']		= $this->User_model->get_name_list();
		
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('select'=>2,'where'=>array('borrow_id'=>$id)));
		
		$borrow_tender_list1 = array();
		
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$borrow_tender_list1[] 		= $tender;
		}
		
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		
		//用户信息
		$user_id = $borrow_obj['user_id'];
		$user_obj = $this->User_model->get_by_uid($user_id);
		$this->load->model('Invest_model');
		
		//print_r($user_id);exit;
		if(isset($user_obj) && !empty($user_obj)){
			$pic = $this->Invest_model->get_invest_rank_by_id($user_obj['invest_rank_id']);
			$borrow_obj['vip_pic'] = $pic;
		}
		// 20150430 标的名称
		$type_arrs = $this->Borrow_model->arrays("borrow_type");
		$borrow_obj['ftype_name']  = $type_arrs[$borrow_obj['ftype']];
		
		$vdata['borrow'] = $borrow_obj;
		
		// 前台图片
		$edition = $borrow_obj['bill_edition'];
		
		$vdata['edition_list']  = explode(",",$edition);
		
		$vdata['user_obj'] = $user_obj;
		
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$this->load->model('User_vip_model');
		$vip = $this->User_vip_model->get_by_uid($borrow_obj['user_id']);
		if(isset($vip['vip_status'])&&$vip['vip_status'] == 1){
			$vdata['kefu'] = $this->User_model->get_by_uid($vip['kefu_userid']);
		}
		$this->load->model('Attestation_type_model');
		$vdata['attestation_type_list'] = $this->Attestation_type_model->get_id_names();
		$this->load->model('Linkage_model');
		$vdata['linkage'] = $this->Linkage_model->get_linkage_list();
		$vdata['title'] 					= $borrow_obj['title'].' - '.CON_KEYWORDS;
		$vdata['tab'] 					= "tender";
		
		
		// 级联菜单
		$vdata['sub_1'] = " 理财产品 ";
		$vdata['sub_2'] = " 理财产品列表 ";
		$vdata['sub_3'] = " 详情 ";
		
		// 级联菜单URI
		$vdata['sub_1_href'] = "/borrow/zhs_blist.html";
		$vdata['sub_2_href'] = "/borrow/zhs_blist.html";
		$vdata['sub_3_href'] = "/borrow/zhs_obj/{$id}";
				
		
		//防止重复提交
		$vdata['smscode'] = rand_string ( $user ['id'], 6, 3, 13 );
		$this->session->set_userdata('smscode'.$user ['id'], $vdata['smscode']);
		
		$this->load->view('borrow/zhs_borrow_obj', $vdata);
	}
	
	
	/**
	 * 投标
	 */
	function zhs_tender(){
		//接收参数
		if($this->session->userdata('rsh_user') == false){//如果未登录就跳到登录页面
			echo '请先登录';
			return;
		}
		$user = $this->session->userdata('rsh_user');
		$id = $this->input->post("id");
		
		// 20150119 by liub 专属标处理
		$captive_user_id = self::captiveBorrow($id);
		if( isset($captive_user_id) && isset($user['user_id']) && $captive_user_id != $user['user_id']){
			//echo "本标为!@#$%标的新闻发布会专属标";
			//return;
		}
		
		$account 	= $this->input->post("account",true);
		$paypassword 	= $this->input->post("paypassword",true);
		$red_money	= $this->input->post("red_money",true);

		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');

		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		
		if(!$borrow_obj){
			$this->js->alert('理财标不存在');
			$this->js->go('/');
			return;
		}
		$user = $this->User_model->get_username_by_id($user['user_id']);
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		
		//TODO 条件修改
		if($user['paypassword']  == ""){
			echo "请先设置支付密码";
			return;
		}
		if($user['real_status']  != "1"){
			echo "请先通过实名认证";
			return;
		}
		if(!$this->myformcheck->isNumber($account)){
			echo "投标金额必须为整数，且为标的最小投资金额的整数倍";
			return;
		}
		if($account % $borrow_obj['lowest_account'] !=0){
			echo "投标金额必须为标的最小投资金额的整数倍";
			return;
		}
		if($pay_result == '' || $pay_result == 0){
			echo "支付密码不正确";
			return;
		}
		if($borrow_obj['fstatus'] != 1){
			echo "此标不能投";
			return;
		}
		
		/*** TODO 20140109 by liub 注释 启标时间
		if((time()-$borrow_obj['bill_starttime']) > 0){
			echo "此标已过启标时间";
			return;
		}*/
		
		if($borrow_obj['account'] <= $borrow_obj['account_yes']){
			echo "此标已满";
			return;
		}
		
		if($borrow_obj['lowest_account'] > $account){
			echo "请输入正确的金额，最小投标金额为".$borrow_obj['lowest_account']."元";
			return;
		}
		
		if(($borrow_obj['most_account'] > 0) && ($account > $borrow_obj['most_account'])){
			echo "投标金额大于最大投标金额";
			return;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			echo "不能投自己的标";
			return;
		}
		// 如果用户投资的金额
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		// 封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];

		$data['borrow_id'] 	= $id;
		
		$user_account = $this->Account_model->get_account($user['user_id']);

		if($account > $user_account['use_money']){
			echo "您的余额不足,请先充值";
			return;
		}
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		//使用代金卷
		if(isset($red_money) && $red_money == "1"){
			$red = floor($account / 200);
			$red = max(0, $red);
			$red = min($red, $user_account['red_money']);
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*200;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$key_red = floor($key_money/200);
			$red = min($key_red,$red);
			/*end */
			$data['red_money'] 	= $red;
		}
		$data['account'] 	= $account;
		//防止重复提交订单
		$local_smscode = trim($this->input->post("smscode"));
		$smscode = $this->session->userdata('smscode'.$user ['id']);
		if($local_smscode == $smscode){
			$this->session->unset_userdata('smscode'.$user ['id']);
			//****** 20141208 by liub 投标(兼容及时、起始计息)
			$flag = $this->Borrow_model->borrow_tender($data);
			//exit;
			if($flag == 1){
				// 发放代金券
				$this->load->model('Redmanagement_model', 'redmanagement');
				$this->redmanagement->send_red($user['user_id'],6,"投标".$id."期获得奖励");
				$title = "<a  href='/borrow/zhs_obj/".$data['borrow_id']."'>第".$data['borrow_id']."期</a>";
				$this->send_msg(array('user_id'=>$user['user_id'],'sys_id'=>25,'money'=>$account,'title'=>$title));//购买成功站内信
				if($user['email']){
					$email_title='民间借款及担保合同';
					$email_content='';
					$this->send_email(array('email'=>$user['email'],'title'=>$email_title,'content'=>$email_content));//发送邮箱
				}
				
				echo 1;	
			}else{
				echo "投标失败";	
			}
		}
	}	
	
	
		// 专属标操作
	private  function captiveBorrow($id){
		//TODO  暂时将匹配规则写入配置文件中，之后可移入数据库
		$this->load->model("Tender_model","td");
		
		return $this->td->getCaptiveUser($id);
	}
	
	/**
	 * 查询余额(服务端接口)
	 */
	private function queryBalance($user_id){
		$config['user_id'] 		= null;
		$this->load->library('baofooSdk/dto/request/BalanceDto',$config,'balanceDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br_recharge');
		// 赋值
		$this->balanceDto->__set("user_id",$user_id);
		return $this->br_recharge->api_AccountBalance($this->balanceDto);
	}	
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 标
 * @author chenhuan
 *
 */
class Borrow extends MY_Controller {
    
	function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 标列表
	 * @param $qtype 查询类型
	 */
	function zhs_blist(){
		//搜索参数
		/* $per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$keywords 			= $this->input->get('keywords');
		$time_limit 		= $this->input->get('time_limit');
		$ftype 				= $this->input->get('ftype'); */
		
		$postArr = array(
			'sftype' => intval($this->input->get('sftype')),//标的类型
			'saccount' => intval($this->input->get('saccount')),//金额
			'sapr' => intval($this->input->get('sapr')),//年化利率
			'slimit' => intval($this->input->get('slimit')),//期限
			'style' => intval($this->input->get('style')),//还款方式
			'srate' => intval($this->input->get('srate')),//还款方式
			'sorder' => $this->input->get('sorder'),//
		);
		
		$postArr['sorder'] = $postArr['sorder']?$postArr['sorder']:'all';
		
		$vdata['postArr'] = $postArr;
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		$this->load->model('Credit_model');
		
		
		
		$data['where']['sftype'] 		= $postArr['sftype'];
		$data['where']['saccount'] 		= $postArr['saccount'];
		$data['where']['sapr'] 			= $postArr['sapr'];
		$data['where']['slimit'] 		= $postArr['slimit'];
		$data['where']['style'] 		= $postArr['style'];
		$data['where']['srate'] 		= $postArr['srate'];
		$data['where']['sorder'] 		= $postArr['sorder'];
		$per_page = $this->input->get('per_page')?intval($this->input->get('per_page')):0;
		$data['page_start'] = $per_page;//intval($page);
		$data['page_size'] = 10;
		$data['list'] 					= 0;
		
		$retData = $this->Borrow_model->get_borrow_blist($data);
		$vdata['borrow_list'] = $retData['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/borrow/zhs_blist.html?sftype='.$postArr['sftype'].'&saccount='.$postArr['saccount'].'&sapr='.$postArr['sapr'].'&slimit='.$postArr['slimit'].'&style='.$postArr['style'].'&srate='.$postArr['srate'];
		$config['total_rows'] = $retData['borrow_count'];
		$config['per_page'] = $data['page_size']; 
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		
		//获取用户资金信息
		if($this->session->userdata('rsh_user') != null){
			$user = $this->session->userdata('rsh_user');
			$this->load->model('Account_model');
			$vdata['user_account'] 	= $this->Account_model->get_account($user['user_id']);
			$tuser_account 			= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}else{
				$vdata['user_paypassword'] = $tuser_account['paypassword'];
			}
		}
		$borrow_list = array();
		foreach ($vdata['borrow_list'] as $borrow){
			if ($borrow['ftype']!=6){
				$user_id = $borrow['user_id'];
				$user_obj = $this->User_model->get_by_uid($user_id);
				$borrow['username'] = $user_obj['username'];
				$borrow['province'] = $user_obj['province'];
				$borrow['city'] 	= $user_obj['city'];
				$borrow['area'] 	= $user_obj['area'];
				$credit_obj = $this->Credit_model->get_row($user_id);
				$credit_obj['credit_value'] = isset($credit_obj) ? $credit_obj['credit_value'] : 0;
				$borrow['user_credit'] 		= $credit_obj['credit_value'];
				$borrow['user_credit_img'] 	= $this->Credit_model->get_pic($credit_obj['credit_value']);
				$borrow_list[] 		= $borrow;
			}
		}
		
		//获取地区信息
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$vdata['borrow_list']				= $borrow_list;
		
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_account'] 			= $this->Borrow_model->arrays('borrow_account');// 理财金额
		$vdata['borrow_status'] 			= $this->Borrow_model->arrays('borrow_status');//状态
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
		$vdata['borrow_time_limit_query'] 		= $this->Borrow_model->arrays('borrow_time_limit_query');//理财期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		/**
			$vdata['time_limit'] 	= $time_limit;
			$vdata['ftype'] 		= $ftype;
			$vdata['qtype'] 		= $qtype;
			$vdata['keywords'] 		= $keywords;
		*/
		
		$vdata['title'] 		= "理财产品".' - '.CON_KEYWORDS;
		$vdata['tab'] 		= "tender";
		
		$vdata['all_order'] = 'all';
		$vdata['account_order'] = $postArr['sorder'] && $postArr['sorder']=="account_down"? 'account_up':'account_down';
		$vdata['apr_order'] = $postArr['sorder'] && $postArr['sorder']=="apr_down"? 'apr_up':'apr_down';
		$vdata['timelimit_order'] = $postArr['sorder'] && $postArr['sorder']=="timelimit_down"? 'timelimit_up':'timelimit_down';
		
		// 级联菜单
		$vdata['sub_1'] = "理财产品列表";
		$vdata['sub_1_href'] = "/borrow/zhs_blist.html";
			
		$this->load->view('borrow/zhs_borrow_list', $vdata);
	}
	
	
	/**
	 * 标列表
	 * @param $qtype 查询类型
	 */
	function blate($qtype = 'late'){
		//搜索参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$keywords 			= $this->input->get('keywords');
		$time_limit 		= $this->input->get('time_limit');
		$ftype 				= $this->input->get('ftype');
		
		$data['page_size']  			= BORROW_LIST_PAGE_SIZE;
		$data['page_start'] 			= (int)$per_page;
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_late_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/borrow/blate/index.html';
		$config['total_rows'] = $vdata['late_count'];
		$config['per_page'] = BORROW_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		
		//获取地区信息
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//理财期限
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//理财期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_valid_time'] 		= $this->Borrow_model->arrays('borrow_valid_time');//有效时间
		$vdata['qtype'] 		= "late";
		$vdata['time_limit'] 	= $time_limit;
		$vdata['ftype'] 		= $ftype;
		$vdata['keywords'] 		= $keywords;
		$vdata['title'] 		= "我要理财".' - '.CON_KEYWORDS;
		$this->load->view('borrow/borrow_list', $vdata);
	}
	

	//20141129 by liub 查看标的详情*********************
	//****** 同步更新  zhs_obj 方法
	function obj($id){
		self::zhs_obj($id);
	}
	
	

	//20141129 by liub 查看标的详情
	function zhs_obj($id){
		
		$this->load->model('User_model');
		$this->load->model('User_info_model');
		$this->load->model('Area_model', 'area');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		$this->load->model('Credit_model');
		$this->load->model('User_amount_model');
		$this->load->model('Attestation_model');
		
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_work_time'] 			= $this->Borrow_model->arrays('borrow_work_time');//计息方式
		$vdata['borrow_fuse'] 				= $this->Borrow_model->arrays('borrow_fuse');//资金用途
		$vdata['borrow_good'] 				= $this->Borrow_model->arrays('borrow_good');
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		if(!$borrow_obj){
			$this->js->alert('理财标不存在');
			$this->js->go('/');
			return;
		}
		if($borrow_obj['fstatus'] !=1 && $borrow_obj['fstatus'] !=3){
			$this->js->alert("您没有权限访问该理财标");
			$this->js->go("/borrow/zhs_blist/index.html");
			return;
		}
		$vdata['borrow_style'] 		= $this->Borrow_model->arrays('borrow_style');//还款方式
		$user = $this->session->userdata('rsh_user');

		//修改标的点击次数
		//$this->Borrow_model->update_hits($id);
	
		$user_account = array();
		$tuser_account = array();
		$vdata['key_red']  = 0;//可激活代金卷
		if($user == null){
			$user_account['total'] 		= 0;
			$user_account['use_money'] 	= 0;
			$tuser_account['paypassword'] = "";
		}else{
			$user_account 					= $this->Account_model->get_account($user['user_id']);
			if($user_account == false){
				$user_account['total'] 		= 0;
				$user_account['use_money'] 	= 0;
			}
			$tuser_account 					= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*100;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$vdata['key_red'] = floor($key_money/200);
			if(!isset($user_account['red_money'])) $user_account['red_money'] =0;
			/*end */
		}
		
			
		$vdata['user_account']			= $user_account;
		
		$vdata['tuser_account']			= $tuser_account;
		//TODO 20150414 疑问
		//$vdata['user_name_list']		= $this->User_model->get_name_list();
		
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('select'=>2,'where'=>array('borrow_id'=>$id)));
		
		$borrow_tender_list1 = array();
		
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$borrow_tender_list1[] 		= $tender;
		}
		
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		
		//标的安全等级
		$this->load->model('Conset_model');
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		
		//用户信息
		$user_id = $borrow_obj['user_id'];
		$user_obj = $this->User_model->get_by_uid($user_id);
		$this->load->model('Invest_model');
		
		//print_r($user_id);exit;
		if(isset($user_obj) && !empty($user_obj)){
			$pic = $this->Invest_model->get_invest_rank_by_id($user_obj['invest_rank_id']);
			$borrow_obj['vip_pic'] = $pic;
		}
		// 20150430 标的名称
		$type_arrs = $this->Borrow_model->arrays("borrow_type");
		$borrow_obj['ftype_name']  = $type_arrs[$borrow_obj['ftype']];
		
		$vdata['borrow'] = $borrow_obj;
		
		// 前台图片
		$edition = $borrow_obj['bill_edition'];
		
		$vdata['edition_list']  = explode(",",$edition);
		
		$vdata['user_obj'] = $user_obj;
		
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$this->load->model('User_vip_model');
		$vip = $this->User_vip_model->get_by_uid($borrow_obj['user_id']);
		if(isset($vip['vip_status'])&&$vip['vip_status'] == 1){
			$vdata['kefu'] = $this->User_model->get_by_uid($vip['kefu_userid']);
		}
		$this->load->model('Attestation_type_model');
		$vdata['attestation_type_list'] = $this->Attestation_type_model->get_id_names();
		$this->load->model('Linkage_model');
		$vdata['linkage'] = $this->Linkage_model->get_linkage_list();
		$vdata['title'] 					= $borrow_obj['title'].' - '.CON_KEYWORDS;
		$vdata['tab'] 					= "tender";
		
		
		// 级联菜单
		$vdata['sub_1'] = " 理财产品 ";
		$vdata['sub_2'] = " 理财产品列表 ";
		$vdata['sub_3'] = " 详情 ";
		
		// 级联菜单URI
		$vdata['sub_1_href'] = "/borrow/zhs_blist.html";
		$vdata['sub_2_href'] = "/borrow/zhs_blist.html";
		$vdata['sub_3_href'] = "/borrow/zhs_obj/{$id}";
		
		
		
		//防止重复提交
		$vdata['smscode'] = rand_string ( $user ['id'], 6, 3, 13 );
		$this->session->set_userdata ( 'smscode'.$user ['id'], $vdata['smscode']);
		
		$this->load->view('borrow/zhs_borrow_obj', $vdata);
	}
	
	//股指期货
	//20150814
	function zhs_qihuo(){
		
		if($_POST){
			if($this->session->userdata('rsh_user') == false){//如果未登录就跳到登录页面
				echo '请先登录';
				return;
			}
			$user = $this->session->userdata('rsh_user');
			$this->load->model('qihuo_model');
			$this->load->model('account_model');
			$res_acc=$this->account_model->get_account($user['user_id']);
			if($res_acc['use_money']<$_POST['money']){
				echo '您的余额不足，请先充值';
				return;
			}
			$data['userid']=$user['user_id'];
			$data['ftype']=1;//类型
			$data['money']=$_POST['money'];
			$data['is_check']=0;//是否审核
			$data['addtime']=time();
			$data['remark']='';//备注
			$res=$this->qihuo_model->add($data);
			if($res==1){
				echo '申请成功，请等待审核';
			}else{
				echo '申请失败，请联系客服';
			}			
			return;
		}

		$this->load->view('borrow/zhs_qihuo');
	}

	//股指期货合作协议
	//20150816
	function zhs_qihuo_contract(){
		$this->load->view('borrow/zhs_qihuo_contract');
	}

	/**
	 * 投标
	 */
	function zhs_tender(){
		//接收参数
		if($this->session->userdata('rsh_user') == false){//如果未登录就跳到登录页面
			echo '请先登录';
			return;
		}
		$user = $this->session->userdata('rsh_user');
		$id 				= $this->input->post("id");
		
		// 20150119 by liub 专属标处理
		$captive_user_id = self::captiveBorrow($id);
		if( isset($captive_user_id) && isset($user['user_id']) && $captive_user_id != $user['user_id']){
			//echo "本标为!@#$%标的新闻发布会专属标";
			//return;
		}
		
		$account 			= $this->input->post("account",true);
		$paypassword 		= $this->input->post("paypassword",true);
		$red_money			= $this->input->post("red_money",true);

		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		if(!$borrow_obj){
			$this->js->alert('理财标不存在');
			$this->js->go('/');
			return;
		}
		$user = $this->User_model->get_username_by_id($user['user_id']);
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		
		//TODO 条件修改
		if($user['paypassword']  == ""){
			echo "请先设置支付密码";
			return;
		}
		if($user['real_status']  != "1"){
			echo "请先通过实名认证";
			return;
		}
		if(!$this->myformcheck->isNumber($account)){
			echo "投标金额必须为整数，且为标的最小投资金额的整数倍";
			return;
		}
		/*20150715  @zhu  改动
		if($account % $borrow_obj['lowest_account'] !=0){
			echo "投标金额必须为标的最小投资金额的整数倍";
			return;
		}*/
		if($pay_result == '' || $pay_result == 0){
			echo "支付密码不正确";
			return;
		}
		if($borrow_obj['fstatus'] != 1){
			echo "此标不能投";
			return;
		}
		
		/*** TODO 20140109 by liub 注释 启标时间
		if((time()-$borrow_obj['bill_starttime']) > 0){
			echo "此标已过启标时间";
			return;
		}*/
		
		if($borrow_obj['account'] <= $borrow_obj['account_yes']){
			echo "此标已满";
			return;
		}
		
		if($borrow_obj['lowest_account'] > $account){
			echo "请输入正确的金额，最小投标金额为".$borrow_obj['lowest_account']."元";
			return;
		}
		
		if(($borrow_obj['most_account'] > 0) && ($account > $borrow_obj['most_account'])){
			echo "投标金额大于最大投标金额";
			return;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			echo "不能投自己的标";
			return;
		}
		// 如果用户投资的金额
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		// 封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];

		$data['borrow_id'] 	= $id;
		
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($account > $user_account['use_money']){
			echo "您的余额不足,请先充值";
			return;
		}
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		//使用代金卷
		if(isset($red_money) && $red_money == "1"){
			$red = floor($account / 200);
			$red = max(0, $red);
			$red = min($red, $user_account['red_money']);
			/*begin tangc 20150104 限制代金卷的激活*/
			//获取激活代金卷总额
			$this->load->model('Accountlog_model', 'accountlog');
			$red_sum = $this->accountlog->get_sum_type($user['user_id'],REDPACKAGE_DEDUCT);
			$red_sum = 0;	//屏蔽代金卷激活次数
			$money_sum  = $red_sum*200;
			$key_money = $user_account['total'] - $money_sum;
			if($key_money > 0){
				if($key_money > $user_account['use_money']) $key_money = $user_account['use_money'];
			}
			else $key_money = 0;
			$key_red = floor($key_money/200);
			$red = min($key_red,$red);
			/*end */
			$data['red_money'] 	= $red;
		}
		$data['account'] 	= $account;
		//防止重复提交订单
		$local_smscode = trim($this->input->post("smscode"));
		$smscode = $this->session->userdata('smscode'.$user ['id']);
		if($local_smscode == $smscode){
			$this->session->unset_userdata('smscode'.$user ['id']);
			//****** 20141208 by liub 投标(兼容及时、起始计息)
			$flag = $this->Borrow_model->borrow_tender($data);
			if($flag == 1){
				// 发放代金券
				$this->load->model('Redmanagement_model', 'redmanagement');
				$this->redmanagement->send_red($user['user_id'],6,"投标".$id."期获得奖励");
				$title = "<a  href='/borrow/zhs_obj/".$data['borrow_id']."'>第".$data['borrow_id']."期</a>";
				$this->send_msg(array('user_id'=>$user['user_id'],'sys_id'=>25,'money'=>$account,'title'=>$title));//购买成功站内信
				echo 1;	
			}else{
				echo "投标失败";	
			}
		}
	}
	
	
	
		// 专属标操作
	private  function captiveBorrow($id){
		//TODO  暂时将匹配规则写入配置文件中，之后可移入数据库
		$this->load->model("Tender_model","td");
		
		return $this->td->getCaptiveUser($id);
	}
	
	/**
	 * 查询余额(服务端接口)
	 */
	private function queryBalance($user_id){
		$config['user_id'] 		= null;
		$this->load->library('baofooSdk/dto/request/BalanceDto',$config,'balanceDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br_recharge');
		// 赋值
		$this->balanceDto->__set("user_id",$user_id);
		return $this->br_recharge->api_AccountBalance($this->balanceDto);
	}
}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
